package test.nettys.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author wangshaopeng
 * @Date 2021/7/16
 */
public class NioTest4 {


    public static void main(String[] args) throws IOException {
        FileInputStream inputStream = new FileInputStream("test/nettys/input.txt");
        FileOutputStream outputStream = new FileOutputStream("test/nettys/output.txt");

        FileChannel inputStreamChannel = inputStream.getChannel();
        FileChannel outputStreamChannel = outputStream.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(4);


        while (true){
            buffer.clear();

            int read = inputStreamChannel.read(buffer);

            if (read==-1) {
                break;
            }
            buffer.flip();
            outputStreamChannel.write(buffer);

        }

        inputStreamChannel.close();
        outputStreamChannel.close();
    }
}
